﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Solidi
{
    sealed class DependencyKeyEqualityComparer : IEqualityComparer<IDependencyKey>
    {
        public static readonly IEqualityComparer<IDependencyKey> Default = new DependencyKeyEqualityComparer();

        public bool Equals(IDependencyKey x, IDependencyKey y)
        {
            bool equal = x.Type == y.Type;

            if (equal)
            {
                if (x.Name == null)
                {
                    return y.Name == null;
                }
                else if (y.Name != null)
                {
                    return x.Name.Equals(y.Name);
                }
            }

            return false;
        }

        public int GetHashCode(IDependencyKey obj)
        {
            int hashCode = obj.Type.GetHashCode();

            if (obj.Name != null)
            {
                hashCode ^= obj.Name.GetHashCode();
            }

            return hashCode;
        }
    }
}
